package com.jihe.ehmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jihe.common.utils.PageUtils;
import com.jihe.common.utils.Query;
import com.jihe.ehmall.product.dao.AttrAttrgroupRelationDao;
import com.jihe.ehmall.product.entity.AttrAttrgroupRelationEntity;
import com.jihe.ehmall.product.service.AttrAttrgroupRelationService;
import com.jihe.ehmall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void removeAttrRelation(AttrGroupRelationVo[] attrGroupRelationVos) {
        //根据属性id和分组id删除关联
        List<AttrGroupRelationVo> attrGroupRelationVoList = Arrays.asList(attrGroupRelationVos);
        attrGroupRelationVoList.forEach(item -> {
            Map<String , Object> map = new HashMap<>();
            map.put("attr_id" , item.getAttrId());
            map.put("attr_group_id" , item.getAttrGroupId());
            this.baseMapper.deleteByMap(map);
        });
    }

    @Override
    public void saveBatchs(List<AttrGroupRelationVo> relationVoList) {
        List<AttrAttrgroupRelationEntity> relationEntityList = relationVoList.stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        this.saveBatch(relationEntityList);
    }


}